package com.xbongbong.saas.model.impl;

import com.xbongbong.paas.domain.entity.ext.PaasFormEntityExt;
import com.xbongbong.paas.enums.DelEnum;
import com.xbongbong.paas.model.PaasFormModel;
import com.xbongbong.paas.toolbox.util.DateUtil;
import com.xbongbong.saas.domain.dao.EsignSetDao;
import com.xbongbong.saas.domain.entity.EsignSetEntity;
import com.xbongbong.saas.enums.RedundantTemplateTypeEnum;
import com.xbongbong.saas.model.EsignSetModel;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * 签署设置
 * 创建时间 2020/6/20 2:03 PM
 * 修改时间 2020/6/20 2:03 PM
 * @author chy
 */
@Service("esignSetModel")
public class EsignSetModelImpl implements EsignSetModel {

    @Resource
    private EsignSetDao esignSetDao;
    @Resource
    private PaasFormModel paasFormModel;

    @Override
    public Integer insert(EsignSetEntity entity) {
        long now = DateUtil.getInt();
        entity.setUpdateTime(now);
        entity.setAddTime(now);
        entity.setDel(DelEnum.NORMAL.getDel());
        return esignSetDao.insert(entity);
    }

    @Override
    public Integer update(EsignSetEntity entity) {
        entity.setUpdateTime((long)(DateUtil.getInt()));
        return esignSetDao.update(entity);
    }

    @Override
    public Integer save(EsignSetEntity entity) {
        if(entity.getId() == null || Objects.equals(0L, entity.getId())) {
            return insert(entity);
        }
        return update(entity);
    }

    @Override
    public Integer deleteByKey(Long key) {
        return esignSetDao.deleteByKey(key);
    }

    @Override
    public EsignSetEntity getByKey(Long key) {
        return esignSetDao.getByKey(key);
    }

    @Override
    public EsignSetEntity getByFormId(String corpid, Long formId) {
        EsignSetEntity esignSet = esignSetDao.getByFormId(corpid, formId);

        if (Objects.isNull(esignSet)) {
            PaasFormEntityExt paasFormEntityExt = paasFormModel.getByKey(formId, corpid);
            Integer businessType = paasFormEntityExt.getBusinessType();
            RedundantTemplateTypeEnum redundantTemplateTypeEnum = RedundantTemplateTypeEnum.getByCode(businessType);
            if (Objects.equals(redundantTemplateTypeEnum, RedundantTemplateTypeEnum.CONTRACT) ||
            Objects.equals(redundantTemplateTypeEnum, RedundantTemplateTypeEnum.PURCHASE) ||
                    Objects.equals(redundantTemplateTypeEnum, RedundantTemplateTypeEnum.QUOTATION)  ) {
                esignSet = new EsignSetEntity();
                esignSet.setCorpid(corpid);
                esignSet.setFormId(formId);
                esignSet.setEnable(1);
            }
        }
        return esignSet;
    }

    @Override
    public List<EsignSetEntity> findEntitys(Map<String ,Object> param) {
        return esignSetDao.findEntitys(param);
    }

    @Override
    public Integer getEntitysCount(Map<String ,Object>  param) {
        return esignSetDao.getEntitysCount(param);
    }

    @Override
    public List findEntitysWithoutSub(Map<String, Object> param) {
        return null;
    }
}
